<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/5/4 9:58 jiahe_new MemberCard.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\common\common\model\oldModel\member;

use app\common\common\model\oldModel\OldBase;

class  memberCard extends OldBase {
    protected $table = 'member_card';
    protected $pk = 'card_id';

    //状态值
    public static $status_on = 1;
    public static $status_off = 2;

    //卡类型
    public static $type_share = 2;
    public static $type_alone = 1;

    //错误消息
    public $error = '';
    //状态值
    public $status = true;
    //其他附带信息
    public $data = [];



    /**
     * 不带分页的多条查询
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function allByWhere($where){
        return $this->where($where)->selectToArray();
    }


    /**
     * 查询单条数据
     * @param $where
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findByWhere($where){
        return $this->where($where)->findToArray();
    }


    /**
     * 修改月卡次数
     * @param string $member_id 会员Id
     * @param int    $type  类型 1早餐月卡 2早餐共享卡
     * @param int    $num  数量
     */
    public function updateCard( $member_id = 0 , $type = 0  , $num = 0 , $order_id = ''){
        $data = $this->where( array( 'type' => $type , 'member_id' => $member_id , 'status' => 1 ) )->find();
        if( !$data ){
            return false;
        }
        $result = $this->where( array( 'type' => $type , 'member_id' => $member_id , 'status' => 1 ) )->setDec( 'rest_times' , $num );
        if($result !== false ){
            if( $order_id ){
                $this->addCardLog( $data['card_id'] , $member_id , $order_id );
            }
            return true;
        }  else {
            return false;
        }
    }
    public function addCardLog( $card_id = '' , $member_id = '' , $order_id = ''){
        $cardLogModel = new memberCardLog();
        return $cardLogModel->save([
            'card_id' => $card_id ,
            'member_id' => $member_id ,
            'order_id'  => $order_id
        ]);
    }

}  